<?php
/**
 * @author Feng
 * @date 2012-6-1
 */
class controller_admin_picture_photo extends system_user_controller_photo {
		
	public function action_index() {
		$this->_goRoute('picture/photo/list');
		$this->render();
	}
	
	public function action_batch() {
		$concept = new picture_special_concept($this->user->getLang());
		$special_id = intval($this->_param('special_id'));	
		$this->view->step = $step = intval($this->_post('next'));
		if($this->getRequest()->isPost() && $step == 1) {
			$this->referer = $this->_post('referer');
			try {
				$sids = (array) $this->_post('special_id');
				$sids[$special_id] = $special_id;
				$sid = implode(', ', $sids);		
				
				$_photo = (array) $this->_post('photo_id');
				$_photo[] = 0;
				$id = implode(', ', $_photo);

				$this->view->specialList = $concept->fetchAll(array("special.id IN($sid)"));		
				$list = $this->concept->fetchAll(array("photo.id IN($id)"));
				
				$pids = $locale = $keyword = array();
				foreach($list AS $_row) {
					$pids[$_row->getId()] = $_row->getId();
				}
				if(!empty($pids)) {
					$pid = implode(', ', $pids);
					$concept = new picture_photo_locale_concept();
					foreach($concept->fetchAll(array("photo_id IN($pid)", 'lang = ?' => $this->user->getLang())) AS $_row) {
						$locale[$_row->getPhotoId()] = $_row;
					}	
					$_kw = array();		
					$concept = new resource_keyword_concept($this->user->getLang());
					foreach($concept->fetchAll(array("id IN (SELECT `keyword_id` FROM `picture_photo_keyword` WHERE `photo_id` IN($pid))")) AS $_row) {
						$_kw[$_row->getId()] = $_row->getName();
					}
					if(!empty($_kw)) {
						$pid = implode(', ', array_keys($_kw));
						$concept = new picture_photo_keyword_concept();
						foreach($concept->fetchAll(array("keyword_id IN($pid)")) AS $_row) {
							if(!isset($_kw[$_row->getKeywordId()])) continue;
							$keyword[$_row->getPhotoId()][] = $_kw[$_row->getKeywordId()];
						}
					}
				}

				$this->view->locale = $locale;
				$this->view->keyword = $keyword;
				$this->view->list = $list;				
			} catch (Exception $e) {
				$this->view->step = 0;
				$this->view->throwException = $e;
			}
		} elseif($this->getRequest()->isPost() && $step == 2) {
			$this->referer = $this->_post('referer');
			try {				
				foreach((array) $this->_post('photo') AS $id => $_photo) {
					if(!($row = $this->concept->fetch(array('id = ?' => $id)))) {
						continue;
					}
					try {
						system_db::getAdapter()->beginTransaction();
						$row->getLocale()->setTitle(trim($_photo['title']));
						$row->getLocale()->setDescription($_photo['description']);
						$row->setDisplayOrder(intval($_photo['display_order']));
						$row->setKeywords($_photo['tag']);
						$row->save(false);
						system_db::getAdapter()->commit();
					} catch (Exception $ex) {
						system_db::getAdapter()->rollBack();
					}
				}
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->step = 0;
				$this->view->throwException = $e;
			}
		} else {
			$specialList = array();
			$isInList = false;
			foreach($concept->fetchAll(array(), array('special.creationtime DESC'), 10) AS $row) {
				if($special_id == $row->getId()) $isInList = true;
				$specialList[] = $row;
			}
			if(!$isInList && $row = $concept->fetch(array('special.id = ?' => $special_id))) {
				array_unshift($specialList, $row);
			}
			$this->view->specialList = $specialList;
		}
		$this->view->special_id = $special_id;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('UPLOAD IN BATCHES'))->setRoute('picture/photo/batch')->setParams(array('special_id' => $special_id)));
		$this->render();
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$row->setIsActive(1);
		$row->setDisplayOrder(0);
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Photo'))->setRoute('picture/photo/addnew'));
		$this->render('picture/photo/row.phtml');
	}
	
	public function action_edit() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('photo.id = ?' => $id)))) {
			throw new picture_exception("The Photo is not Existing.");
		}		
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($row->getLocale()->getTitle())->setRoute('picture/photo/edit')->setParams(array('id' => $row->getId())));
		$this->render('picture/photo/row.phtml');
	}
	
	public function action_list() {
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('picture/photo/list'));
		$checkedList = array();
		$render = null;
		if($this->getRequest()->isPost() && $this->_post('addTo')) {
			$specialList = array();
			$checkedList =  (array) $this->_post('photo_id');
			$this->_crumbs()->addPage(system_nav_page::create($this->language('Add Photo To Special'))->setRoute('picture/photo/list'));
			try {
				system_db::getAdapter()->beginTransaction();
				if(empty($checkedList)) {
					throw new picture_exception("No Photo selected.");
				}
				$concept = new picture_photo_concept($this->user->getLang());
				$_id = implode(',', $checkedList);
				$this->view->photos = $concept->fetchAll(array("photo.id IN($_id)"));
				
				$render = 'picture/photo/addToSpecial.phtml';
				if($this->_post('addToSpecial')) {
					$specialId =  (array) $this->_post('special_id');
					if(empty($specialId)) {
						throw new picture_exception("No Special selected.");
					}
					$_id = implode(',', $specialId);
					$concept = new picture_special_concept($this->user->getLang());
					$specialList = $concept->fetchAll(array("special.id IN($_id)"));
					
					$sql = "REPLACE INTO `picture_special_photo`(`special_id`, `photo_id`) VALUES";
					$_sql = array();
					foreach($checkedList AS $pid) {
						foreach($specialId AS $sid) {
							$_sql[] = "('$sid', '$pid')";
						}
					}
					$sql .= implode(',', $_sql);
					system_db::getAdapter()->query($sql);
					
					$_id = implode(',', $specialId);
					$sql = "UPDATE `picture_special` AS `a` SET `ptotal` = (
							SELECT COUNT(1) FROM `picture_special_photo` WHERE `special_id` = a.`id`
						) WHERE a.`id` IN($_id)";
					system_db::getAdapter()->query($sql);
					system_db::getAdapter()->commit();
					$this->view->throwMessage = 'Complete';					
				}
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
			$this->view->specialList = $specialList;
		}
		if($this->getRequest()->isPost() && $this->_post('delete')) {
			//$this->referer = $this->_post('referer');
			$checkedList = (array) $this->_post('photo_id');
			try {
				system_db::getAdapter()->beginTransaction();
				if(empty($checkedList)) {
					throw new picture_exception("No item Selected.");
				}
				$this->_auth();
				$_pid = implode(', ', $checkedList);				
				foreach($this->concept->fetchAll(array("photo.id IN($_pid)")) AS $photo) {
					// 這里有區分本地化資訊,所以隻刪除本地化資訊
					$photo->delete(array('locale_only' => 1, 'reserve_collection' => 1));
				}
				system_db::getAdapter()->commit();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
		}
		$this->view->checkedList = $checkedList;
		if($render === null) {
			$this->_photoList();
		}
		$this->render($render);
	}
}
